let app = getApp();
let API_URL = app.globalData.API_URL;
import { getHomeVideo } from "../../request/api";

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        lang: null,
        videoList: [],
        changeIndex: 0,
        waiting: true,
        lestTime: 0, // 历时
        videoId: 0, // 分享时的视频 ID
        sharePageNo: 1, // 分享视频 pageNo
        isPlay: true,
        isShare: false,

        // 分页
        pageNo: 1,
        totalPage: 0,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 获取内容
        getPost() {
            let data = { pageNo: this.data.pageNo, pageSize: 5, status: 1 }
            getHomeVideo(res => {
                if (res.statusCode == 200) {
                    this.setData({ videoList: this.data.videoList.concat(res.data.data), totalPage: res.data.totalPage })
                }
            }, data);
        },

        // 返回
        previous() {
            wx.navigateBack({
                delta: 1,
                fail: err => {
                    console.log("err: ", err);
                    wx.navigateTo({
                        url: '/pages/index/index',
                    })
                }
            })
        },

        // 防止速滑
        videoLoaded(e) {
            // console.log("videoLoaded!", e);
            this.setData({ waiting: false })
        },

        //划动切换
        slide(e) {
            this.setData({
                changeIndex: e.detail.current,
                waiting: true,
                isPlay: true
            })
            if (this.data.videoList.length - e.detail.current == 5) {
                if (this.data.sharePageNo == this.data.totalPage) return
                // let pageNo = this.data.sharePageNo += 1
                let pageNo = this.data.videoList.length / 5
                this.setData({ sharePageNo: pageNo })
                if (!this.data.isShare) return;

                let data = { pageNo: this.data.sharePageNo, pageSize: 5, status: 1 }
                getHomeVideo(res => {
                    if (res.statusCode == 200) {
                        this.setData({ videoList: this.data.videoList.concat(res.data.data), totalPage: res.data.totalPage })
                    }
                }, data);
            }
            if (this.data.videoList.length - e.detail.current == 2) {
                if (this.data.pageNo == this.data.totalPage) return
                let pageNo = this.data.pageNo += 1
                this.setData({ pageNo: pageNo })
                this.getPost();
            }
        },

        // 播放控制
        isPlay(e) {
            let videoContext = wx.createVideoContext(e.target.id, this)
            if(this.data.isPlay) {
                videoContext.pause()
                this.setData({ isPlay: false })
            } else {
                videoContext.play()
                this.setData({ isPlay: true })
            }
        },

        // 前往个人视频页面
        videoDetailed(e) {
            let item = e.currentTarget.dataset.item.user;
            let json = JSON.stringify(item);

            wx.navigateTo({
                url: '/pages/home/video/videoDetailed/videoDetailed?json=' + json
            });
        },

        // 点击喜欢
        onClickLike(e) {
            // 当前时间
            let currentTime = new Date().valueOf()

            // 首次请求
            if (this.data.lestTime - currentTime == 0) {
                this.setData({ lestTime: currentTime });
                this.likeFun(e);
            }

            // 次请求
            if (currentTime - this.data.lestTime > 3000) {
                this.setData({ lestTime: currentTime });
                this.likeFun(e);
            }
        },

        // 检查登录状态后调用喜欢方法
        likeFun(e) {
            let promise = new Promise(function (resolve, rej) {
                app.login(res => {
                    resolve(res)
                })
            })

            promise.then(
                resolve => {
                    let videoId = e.currentTarget.dataset.item.id
                    let item = this.data.videoList[e.currentTarget.dataset.index]
                    item.isLike = !item.isLike
                    item.favorite_count = item.favorite_count + (item.isLike ? 1 : -1)
                    this.setData({ videoList: this.data.videoList })
                    let data = { videoId }

                    wx.request({
                        url: `${API_URL}/videoFavorite`,
                        header: app.header(),
                        data: data,
                        method: 'post',
                        success: function (res) {
                            if (res.statusCode == 200) {

                            }
                        }
                    })
                }
            )
        },
        
        // 转发页面时出发
        inShare() {
            let id = wx.getLaunchOptionsSync().query.id;
            let pageNo = wx.getLaunchOptionsSync().query.pageNo;
            let index = wx.getLaunchOptionsSync().query.index;
            
            if(id > 0) {
                let data = { pageNo: pageNo, pageSize: 5, status: 1 }
                getHomeVideo(res => {
                    if (res.statusCode == 200) {
                        this.setData({ videoList: this.data.videoList.concat(res.data.data), totalPage: res.data.totalPage })
                    }
                }, data);
                this.setData({ videoId: id, sharePageNo: pageNo, changeIndex: index, isShare: true })
            }
        }
    },

    lifetimes: {
        attached: function () {
            this.setData({ lang: app.globalData.lang })
            this.inShare()
            if(this.data.videoId == 0) this.getPost()
        }
    }
})
